home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Medal Software 2
/
Gold Medal Software Volume 2 (Gold Medal) (1994).iso
/
prog
/
pclcjs.arj
/
RANDFILE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-29
|
1KB
|
43 lines
/* random_file
accepts a file name mask, returns the name of a file randomly
selected from those meeting the mask filespec via the filename pointer
returns an integer value equal to total number of files meeting this
filespec, or -1 if no files were found */
#include <direct.h>
#include <dos.h>
#include <stdlib.h>
#include <string.h>
int random_file(char *mask, char *filename)
{
/* Variables */
int counter, counter2;
struct ffblk filedata;
/* Code begins here */
/* Seed random number generator */
srand(10);
if (findfirst(mask, &filedata, FA_NORMAL) == 0) {
counter++;
while (findnext(&filedata) == 0)
counter++;
counter=rand() % counter + 1;
/* Reset file finder */
findfirst(mask, &filedata, FA_NORMAL);
for (counter2=1;counter2<counter;counter2++)
findnext(&filedata);
strcpy(filename,filedata.ff_name);
return counter;
}
else
return -1;
}